﻿using System;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Joints;
using FarseerPhysics.Factories;
using JilyGameFramework.Controls;
using JilyGameFramework.Manager;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace FarseerPhysics.Samples.ScreenSystem
{
    public class PhysicsGameScreen : GameScreen
    {
        protected World World;
        protected Body HiddenBody;

        private float _agentForce;
        private float _agentTorque;
        private FixedMouseJoint _fixedMouseJoint;
        protected Body _userAgent;

        protected PhysicsGameScreen()
        {

            EnableCameraControl = true;
            _userAgent = null;
            World = null;

        }

        public bool EnableCameraControl { get; set; }

        protected void SetUserAgent(Body agent, float force, float torque)
        {
            _userAgent = agent;
            _agentForce = force;
            _agentTorque = torque;
        }

        public override void LoadContent()
        {
            base.LoadContent();

            if (World == null)
                World = new World(Vector2.Zero);
            else
                World.Clear();



            HiddenBody = BodyFactory.CreateBody(World, Vector2.Zero);

            // Loading may take a while... so prevent the game from "catching up" once we finished loading
            ScreenManager.Game.ResetElapsedTime();

            this.ScreenManager.Input.OnMouseAction += Input_OnMouseAction;
        }

        bool isstart = false;
        void Input_OnMouseAction(object sender, JilyGameFramework.Classes.MouseDataArgs e)
        {


            if (e.Type == JilyGameFramework.Classes.MouseTpye.Down)
            {
                //if (isstart)
                //{
                //    _userAgent.ApplyForce(new Vector2(-1000, -1000));
                //    _userAgent.ApplyTorque(0);
                //}
                //else
                //{

                //    _userAgent.ApplyForce(new Vector2(1000, 1000));
                //    _userAgent.ApplyTorque(0);
                //}

                //isstart = !isstart;


                Fixture savedFixture = World.TestPoint(ConvertUnits.ToSimUnits(e.Point));
                if (savedFixture != null)
                {
                    Body body = savedFixture.Body;
                    body.IsStatic = true;

                    body.Awake = true;
                    body.SleepingAllowed = false ;
                    _fixedMouseJoint = new FixedMouseJoint(body, e.Point);
                    _fixedMouseJoint.MaxForce = 1000.0f * body.Mass;
                    World.AddJoint(_fixedMouseJoint);
                   // body.Awake = true;
                }
            }

            if (e.Type == JilyGameFramework.Classes.MouseTpye.Move)
            {
                //Body body = World.BodyList[2];
                //body.Position = ConvertUnits.ToSimUnits(e.Point);

                Fixture savedFixture = World.TestPoint(ConvertUnits.ToSimUnits(e.Point));
                if (savedFixture != null)
                {
                    Body body = savedFixture.Body;
                    body.Position = ConvertUnits.ToSimUnits(e.Point);
                  //  body.Awake = true;
                }
            }
        }

        public override void Update(GameTime gameTime)
        {

            // variable time step but never less then 30 Hz
            World.Step(Math.Min((float)gameTime.ElapsedGameTime.TotalSeconds, (1f / 30f)));


            base.Update(gameTime);
        }


        //private void HandleCursor(InputHelper input)
        //{
        //Vector2 position = Camera.ConvertScreenToWorld(input.Cursor);

        //if ((input.IsNewButtonPress(Buttons.A) || input.IsNewMouseButtonPress(MouseButtons.LeftButton)) && _fixedMouseJoint == null)
        //{
        //    Fixture savedFixture = World.TestPoint(position);
        //    if (savedFixture != null)
        //    {
        //        Body body = savedFixture.Body;
        //        _fixedMouseJoint = new FixedMouseJoint(body, position);
        //        _fixedMouseJoint.MaxForce = 1000.0f * body.Mass;
        //        World.AddJoint(_fixedMouseJoint);
        //        body.Awake = true;
        //    }
        //}

        //if ((input.IsNewButtonRelease(Buttons.A) || input.IsNewMouseButtonRelease(MouseButtons.LeftButton)) && _fixedMouseJoint != null)
        //{
        //    World.RemoveJoint(_fixedMouseJoint);
        //    _fixedMouseJoint = null;
        //}

        //if (_fixedMouseJoint != null)
        //    _fixedMouseJoint.WorldAnchorB = position;
        //}



        //private void HandleUserAgent(InputHelper input)
        //{
        //    Vector2 force = _agentForce * new Vector2(input.GamePadState.ThumbSticks.Right.X, -input.GamePadState.ThumbSticks.Right.Y);
        //    float torque = _agentTorque * (input.GamePadState.Triggers.Right - input.GamePadState.Triggers.Left);

        //    _userAgent.ApplyForce(force);
        //    _userAgent.ApplyTorque(torque);

        //    float forceAmount = _agentForce * 0.6f;

        //    force = Vector2.Zero;
        //    torque = 0;

        //    if (input.KeyboardState.IsKeyDown(Keys.A))
        //        force += new Vector2(-forceAmount, 0);
        //    if (input.KeyboardState.IsKeyDown(Keys.S))
        //        force += new Vector2(0, forceAmount);
        //    if (input.KeyboardState.IsKeyDown(Keys.D))
        //        force += new Vector2(forceAmount, 0);
        //    if (input.KeyboardState.IsKeyDown(Keys.W))
        //        force += new Vector2(0, -forceAmount);
        //    if (input.KeyboardState.IsKeyDown(Keys.Q))
        //        torque -= _agentTorque;
        //    if (input.KeyboardState.IsKeyDown(Keys.E))
        //        torque += _agentTorque;

        //    _userAgent.ApplyForce(force);
        //    _userAgent.ApplyTorque(torque);
        //}

        private void EnableOrDisableFlag(DebugViewFlags flag)
        {

        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }
    }
}